package com.ktmall.ktmalluijava.controller;

import com.ktmall.ktmalluijava.pojo.Order;
import com.ktmall.ktmalluijava.service.OrderService;
import com.ktmall.ktmalluijava.utils.Page;
import com.ktmall.ktmalluijava.utils.ResMsg;
import com.ktmall.ktmalluijava.vo.OrderDetailVO;
import com.ktmall.ktmalluijava.vo.OrderVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShuang
 * @version 17
 * @Title
 * @date 2025/5/20 上午10:10
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService service;

    @GetMapping("/list")
    public String selectPage(String orderSn, Integer userId, Integer orderState, @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,@RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize){
        Page<OrderVO> page = service.getPage(orderSn,userId,orderState,pageNo,pageSize);
        return ResMsg.success("订单列表加载成功",page);
    }

    @GetMapping("/detail")
    public String detail(Integer id){
        Order order ;
        try{
            order = service.detail(id);
        }catch(Exception e){
            return ResMsg.error("订单详情加载失败",e);
        }
        return ResMsg.success("订单详情加载成功",order);
    }

    @PostMapping("/update")
    public String update(@RequestBody Order order){
        int i = service.update(order);
        if(i>0){
            return ResMsg.success("修改成功");
        }else{
            return ResMsg.error("修改失败");
        }
    }

    @PostMapping("/del")
    public String delete(@RequestBody Integer... id){
        int i = service.delete(id);
        if(i>0){
            return ResMsg.success("删除成功");
        }else{
            return ResMsg.error("删除失败");
        }
    }

    @GetMapping("/orderDetail")
    public String OrderDetail(Integer id){
        OrderDetailVO orderDetailVO = service.orderDetail(id);
        return ResMsg.success("订单详细信息加载成功",orderDetailVO);
    }
}
